#include <WinSock2.h>
#include <WS2tcpip.h>
#include <stdio.h>


#define string char *


void sendMessage(int socketen);
void writeServerLog(string ip, string recieved, string response, int sizeOfResponse);


main(){
        WORD wVersionRequested;
        WSADATA wsaData;
        int err, ok, clientAddrLen, serverSocket, clientSocket;
        struct addrinfo *info;
        struct sockaddr clientAddr;


        wVersionRequested = MAKEWORD(2, 2);
        err = WSAStartup(wVersionRequested, &wsaData);


        //Creates a socket with 3 pre-defined values found in the libraries.
        serverSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
        ok = getaddrinfo("127.0.0.1", "8080", NULL, &info);
        if(ok!=0) {
                WCHAR * error = gai_strerror(ok);
                printf("%s\n",error);
        }


        printf("Waiting for connection...\n");


        //Binds the port to the socket.
        ok = bind(serverSocket, info->ai_addr, info->ai_addrlen); 
        if(ok == SOCKET_ERROR) {
                err = WSAGetLastError();
                printf("%d\n",err);
        }
        ok = listen(serverSocket, SOMAXCONN);
        if(ok == SOCKET_ERROR) {
                err = WSAGetLastError();
                printf("%d\n",err);
        }


        //Creates new socket after another IP connects.
        clientAddrLen = sizeof(clientAddr);
        clientSocket = accept(serverSocket, &clientAddr, &clientAddrLen);


        printf("Connected!\n");
        
        //Sends a message on the new socket.
        sendMessage(clientSocket);
        printf("Message sent.\n");


        //Writes a log for the server.
        writeServerLog("127.0.0.1", " GET /HTTP1.1 C:/Server/index.html", "index.html", 100);


        getchar(); //Waiting for input, Used to stop the server before terminating.


        //Cleanup.
        closesocket(serverSocket);
        closesocket(clientSocket);
        WSACleanup();
}


void sendMessage(int socketen)
{
	char *message, buffer[512];
	int iResult;

	WSADATA wsaData; 
	WORD wVersionRequested = MAKEWORD( 2, 2 ); 
	int err = WSAStartup( wVersionRequested, &wsaData );

	SOCKET s = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);

	struct addrinfo *info; 
	int ok = getaddrinfo("www.bt.se","80",NULL,&info);

	if(ok!=0) { 
		WCHAR * error = gai_strerror(ok); 
		printf("%s\n",error); 
	} else while(info->ai_family != AF_INET && info->ai_next != NULL) 
		info = info->ai_next;

	ok = connect(s, info->ai_addr, info->ai_addrlen);

	message = "GET / HTTP/1.1\r\nHOST: www.bt.se\r\n\r\n"; 
	ok = send(s,message,strlen(message),0);

	iResult = recv(s, buffer, 512, 0);

	while(iResult != 0)
	{
		send(socketen, buffer, strlen(buffer), 0);
		iResult = recv(s, buffer, 512, 0);
	}
}


void writeServerLog(string ip, string recieved, string response, int sizeOfResponse){
        FILE *log;
        SYSTEMTIME st;


        GetSystemTime(&st);
        log = fopen("serverlog.txt", "a");
        fprintf(log, "IP: %s DaTi: %d-%d-%d %d:%d:%d Recv: %s Resp: %s Size: %d\n", ip, st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond, recieved, response, sizeOfResponse);
        fclose(log);
}